package com.wordLearning.word.application.controller;

import com.wordLearning.word.application.convert.FavoriteDTOConverter;
import com.wordLearning.word.application.convert.PlanDTOConverter;
import com.wordLearning.word.application.convert.UnmasterDTOConverter;
import com.wordLearning.word.application.dto.FavoriteWordDTO;
import com.wordLearning.word.application.dto.PlanDTO;
import com.wordLearning.word.application.dto.UnmasteredWordDTO;
import com.wordLearning.word.application.dto.WordDTO;
import com.wordLearning.word.common.entity.PageResult;
import com.wordLearning.word.common.entity.Result;
import com.wordLearning.word.domain.entity.FavoriteWordBO;
import com.wordLearning.word.domain.entity.PlanBO;
import com.wordLearning.word.domain.entity.UnmasteredWordBO;
import com.wordLearning.word.domain.entity.WordBO;
import com.wordLearning.word.domain.service.LearningdomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/learning")
@Slf4j
public class LearningController {

    @Resource
    private LearningdomainService learningdomainService;

    @PostMapping("/setLearningPlan")
    public Result<List<PageResult<WordDTO>>> setLearningPlan(@RequestBody PlanDTO planDTO){
        try{
            int days = planDTO.getDay();
            Integer amount = planDTO.getAmount();
            List<PageResult<WordBO>> result = new ArrayList<>();
            PlanBO planBO = PlanDTOConverter.INSTANCE.convertPlanDTOToBO(planDTO);
            List<PageResult<WordBO>> boPageResult = learningdomainService.setLearningPlan(planBO);


            return Result.ok(boPageResult);
        } catch (Exception e){
            log.error("LearningController.setLearningPlan.error:{}",e.getMessage(),e);
            return Result.fail("设置学习计划失败");
        }
    }


    @PostMapping("/forget")
    public Result<Boolean> forget(@RequestBody WordDTO wordDTO){
        try{
            Integer wordId = wordDTO.getWordId();
            UnmasteredWordDTO unmasteredWordDTO = new UnmasteredWordDTO();
            unmasteredWordDTO.setWordId(wordId);
            unmasteredWordDTO.setUserId(1);
            unmasteredWordDTO.setErrorCount(1);
            unmasteredWordDTO.setAddedAt("2025");
            UnmasteredWordBO unmasteredWordBO = UnmasterDTOConverter.INSTANCE.convertUnmasteredWordDTOToBO(unmasteredWordDTO);
            learningdomainService.forget(unmasteredWordBO);
            return Result.ok("单词标记成功");
        } catch (Exception e){
            log.error("LearningController.forget.error:{}",e.getMessage(),e);
            return Result.fail("单词不认识标记失败");
        }
    }

    @PostMapping("/favorite")
    public Result<Boolean> favorite(@RequestBody WordDTO wordDTO){
        try{
            FavoriteWordDTO favoriteWordDTO = new FavoriteWordDTO();
            favoriteWordDTO.setWordId(wordDTO.getWordId());
            favoriteWordDTO.setAddedAt("2025");
            favoriteWordDTO.setUserId(1);
            FavoriteWordBO favoriteWordBO = FavoriteDTOConverter.INSTANCE.convertFavoriteDTOToBO(favoriteWordDTO);
            learningdomainService.favorite(favoriteWordBO);
            return Result.ok("单词收藏成功");
        } catch (Exception e){
            log.error("LearningController.favorite.error:{}",e.getMessage(),e);
            return Result.fail("单词收藏失败");
        }
    }

    @PostMapping("/remember")
    public Result<Boolean> remember(@RequestBody WordDTO wordDTO){
        try{
            UnmasteredWordDTO unmasteredWordDTO = new UnmasteredWordDTO();
            unmasteredWordDTO.setWordId(wordDTO.getWordId());
            UnmasteredWordBO unmasteredWordBO = UnmasterDTOConverter.INSTANCE.convertUnmasteredWordDTOToBO(unmasteredWordDTO);
            int affectRows = learningdomainService.remember(unmasteredWordBO);
            if (affectRows == 0){
                throw new Exception("WordId " + wordDTO.getWordId() + " Not Found in table unmastered_word");
            }
            return Result.ok("操作成功");
        } catch (Exception e){
            log.error("LearningController.remember.error:{}",e.getMessage(),e);
            return Result.fail("操作失败");
        }
    }

    @PostMapping("/cancelFavorite")
    public Result<Boolean> cancelFavorite(@RequestBody WordDTO wordDTO){
        try{
            FavoriteWordDTO favoriteWordDTO = new FavoriteWordDTO();
            favoriteWordDTO.setWordId(wordDTO.getWordId());
            FavoriteWordBO favoriteWordBO = FavoriteDTOConverter.INSTANCE.convertFavoriteDTOToBO(favoriteWordDTO);
            int affectRows = learningdomainService.cancelFavorite(favoriteWordBO);
            if (affectRows == 0){
                throw new Exception("WordId " + wordDTO.getWordId() + " Not Found in table favorite_word");
            }
            return Result.ok("操作成功");
        } catch (Exception e) {
            log.error("LearningController.cancelFavorite.error:{}",e.getMessage(),e);
            return Result.fail("操作失败");
        }
    }
}
